﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Generic;


namespace Playground.LotteryPrediction
{
    /// <summary>
    /// This program is going to predict the next mega million numbers
    /// </summary>
    public class Prediction
    {

        /// <summary>
        /// constructor of the class
        /// </summary>
        public void Predicion()
        {
            //do nothing for now
        }

        private static SortedDictionary<int, int> gnum = new SortedDictionary<int, int>();
        private static SortedDictionary<int, int> mnum = new SortedDictionary<int, int>();

        /// <summary>
        /// Executes the entire program
        /// </summary>
        public static void Execute()
        {
            int counter = 0;
            StreamReader lotteryData;
            string line = null;
            char[] delimiter = { '-' };
            try
            {
                lotteryData = new StreamReader(@".\LotteryPrediction\data\lottery_data.txt");
                while ((line = lotteryData.ReadLine()) != null)
                {
                    string[] array = null;
                    array = line.Split(delimiter);
                    Aggregate(array);
                }
                lotteryData.Close();
                DisplayResults();
            }
            catch (IOException e)
            {
                Console.WriteLine(
                    "{0}: The write operation could not " +
                    "be performed because the specified " +
                    "part of the file is locked.",
                    e.GetType().Name);
            }
        }

        /// <summary>
        /// Aggregate the data of arrays
        /// </summary>
        /// <param name="array"></param>
        private static void Aggregate(IEnumerable<string> array)
        {
            foreach (var item in array)
            {
                if (item.StartsWith("m"))
                {
                    string pstring = item.Replace("m", "");
                    int power = Convert.ToInt32(pstring);
                    if (mnum.ContainsKey(power))
                    {
                        mnum[Convert.ToInt32(power)]++;
                    }
                    else
                        mnum.Add(power, 1);
                }
                else
                {
                    if (gnum.ContainsKey(Convert.ToInt32(item)))
                    {
                        gnum[Convert.ToInt32(item)]++;
                    }
                    else
                        gnum.Add(Convert.ToInt32(item), 1);
                }
            }
        }

        /// <summary>
        /// Display the results in csv format
        /// </summary>
        private static void DisplayResults()
        {
            TextWriter gtw = new StreamWriter(@"general_numbers.csv");
            SortedDictionary<int, int> mnum2 = mnum;
            gtw.WriteLine("General Numbers,,,Mega Numbers");
            foreach (var gkvp in gnum)
            {
                if (mnum2.Count > 0)
                {
                    gtw.WriteLine("{0},{1},,{2},{3}", gkvp.Key, gkvp.Value, Convert.ToInt32(mnum2.First().Key), Convert.ToInt32(mnum2.First().Value));
                    mnum2.Remove(mnum2.First().Key);
                }
                else
                {
                    gtw.WriteLine("{0},{1}", gkvp.Key, gkvp.Value);
                }
            }
            gtw.Close();
        }
    }
}
